package com.levy.utils.BaiduMap;

/**
 * Created by levy on 2018/7/10.
 */

public class PointCaculateUtils {

    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }

    private static double EARTH_RADIUS = 6378.137;//地球半径


    public static void main(String[] args) {
        //顺时针记录多边形坐标
        //113.62209,23.234412;113.584289,23.222589;113.620796,23.200668;113.645086,23.222058;113.64408,23.238928;
//        Point[] ps = new Point[]{
//                new Point(113.62209,23.234412),
//                new Point(113.584289,23.222589),
//                new Point(113.620796,23.200668),
//                new Point(113.645086,23.222058),
//                new Point(113.64408,23.238928)
//        };
//        Point n1 = new Point(113.59759,23.215303);
//        Point n2 = new Point(113.667175,23.233875);
//        Point n3 = new Point(113.591594,23.222632);
//        System.out.println("n1:" + isPtInPoly(n1, ps));
//        System.out.println("n2:" + isPtInPoly(n2, ps));
//        System.out.println("n3:" + isPtInPoly(n3, ps));


        Point point1 = new Point(113.332587,23.110157);
        Point center = new Point(113.329758, 23.112043);
        double r = 0.440;
        boolean dd = isPtInCircle(point1, center, r);
        System.out.println(dd);


    }


    /**
     * 计算点是否在多边形内
     *
     * @param point
     * @param polygon 多边形数组为按顺序的坐标点
     * @return
     */
    public static boolean isPtInPoly(Point point, Point[] polygon) {
        Double ALon = point.getLng();
        Double ALat = point.getLat();
        int iSum, iCount, iIndex;
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
        if (polygon.length < 3) {
            return false;
        }
        iSum = 0;
        iCount = polygon.length;
        for (iIndex = 0; iIndex < iCount; iIndex++) {
            if (iIndex == iCount - 1) {
                dLon1 = polygon[iIndex].getLng();
                dLat1 = polygon[iIndex].getLat();
                dLon2 = polygon[0].getLng();
                dLat2 = polygon[0].getLat();
            } else {
                dLon1 = polygon[iIndex].getLng();
                dLat1 = polygon[iIndex].getLat();
                dLon2 = polygon[iIndex + 1].getLng();
                dLat2 = polygon[iIndex + 1].getLat();
            }
            // 以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上
            if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    //得到 A点向左射线与边的交点的x坐标：
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);
                    // 如果交点在A点左侧（说明是做射线与 边的交点），则射线与边的全部交点数加一：
                    if (dLon < ALon) {
                        iSum++;
                    }
                }
            }
        }
        if ((iSum % 2) != 0) {
            return true;
        }
        return false;
    }


    /**
     * 点在圆上 也算作在圆内部
     *
     * @param point
     * @param center 圆心
     * @param r      半径 单位KM 三位小数
     * @return
     */
    public static boolean isPtInCircle(Point point, Point center, double r) {
        double distance = getDistance2Points(point, center);
        if (distance > r) {
            return false;
        } else {
            return true;
        }
    }




    /**
     * 返回结果  单位为KM  保留三位小数
     * @param p1
     * @param p2
     * @return
     */
    public static double getDistance2Points(Point p1, Point p2)
    {
        double radLat1 = rad(p1.getLat());
        double radLat2 = rad(p2.getLat());
        double a = radLat1 - radLat2;
        double b = rad(p1.getLng()) - rad(p2.getLng());

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        //保留两位小数
        s = (double)Math.round(s * 1000) / 1000;
        return s;
    }



    /**
     * 返回单位KM 保留三位小数
     */

    public static double getDistance(double lat1, double lng1, double lat2, double lng2)
    {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        //保留两位小数
        s = (double)Math.round(s * 1000) / 1000;
        return s;
    }




}